package gaojie;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Demo3 {
	public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		
		Session session = Session.getInstance(props);
		
		Message msg = new MimeMessage(session);
		MimeMultipart msgMultipart = new MimeMultipart("mixed");
		msg.setContent(msgMultipart);
		
		MimeBodyPart content = new MimeBodyPart();
		MimeBodyPart attch1 = new MimeBodyPart();
		//MimeBodyPart attch2 = new MimeBodyPart();
		
		//msgMultipart.addBodyPart(attch2);
		msgMultipart.addBodyPart(attch1);
		msgMultipart.addBodyPart(content);
		
		DataSource ds = new FileDataSource("D:\\bea\\registry.xml");
		DataHandler dh = new DataHandler(ds);
		attch1.setDataHandler(dh);
		attch1.setFileName("registry.xml");
		
		DataSource ds2 = new FileDataSource("D:/1.jpg");
		DataHandler dh2 = new DataHandler(ds2);
		//attch2.setDataHandler(dh2);
		//attch2.setFileName("1.jpg");
		
		MimeMultipart bodyMultipart = new MimeMultipart("related");
		content.setContent(bodyMultipart);
		MimeBodyPart html = new MimeBodyPart();
		bodyMultipart.addBodyPart(html);
		html.setContent("hello","text/html;charset=gbk");
		msg.saveChanges();
		
		OutputStream ips = new FileOutputStream("D:/test.eml");
		msg.writeTo(ips);
		ips.close();
	}
}
